package cn.kiwipeach.blog.service;

import cn.kiwipeach.blog.domain.AccessTokenVO;
import open.kiwipeach.autoconfigure.shiro.token.AccessToken;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;

/**
 * 描述：用户访问token服务接口
 *
 * @author kiwipeach
 * @since 2.0
 */
public interface IAccessTokenService {

    /**
     * 转换token，同时将该accessToken进行缓存
     *
     * @param accessToken 用户访问token
     * @return 返回缓存的token对象
     */
    @CachePut(value = {"ACCESS_TOKEN"}, key = "#accessToken.id+'::'+#accessToken.refreshToken")
    AccessTokenVO cacheStoreToken(AccessToken accessToken);


    /**
     * 转换token，同时将该accessToken进行缓存
     *
     * @param refreshToken 目标refreshToken
     * @return 返回缓存的token对象
     */
    @Cacheable(value = {"ACCESS_TOKEN"}, key = "#userId+'::'+#refreshToken")
    AccessTokenVO getAccessToken(String userId, String refreshToken);

    /**
     * 移出旧的token
     *
     * @param refreshToken 移出的目标refreshToken
     * @return 移出是否成功
     */
    @CacheEvict(value = {"ACCESS_TOKEN"}, key = "#newAccessToken.id+'::'+#refreshToken")
    @CachePut(value = {"ACCESS_TOKEN"}, key = "#newAccessToken.id+'::'+#newAccessToken.refreshToken")
    AccessTokenVO refreshToken(String refreshToken, AccessToken newAccessToken);


    /**
     * 移出旧的token
     *
     * @param accessToken 移出的目标refreshToken
     * @return 移出是否成功
     */
    @CacheEvict(value = {"ACCESS_TOKEN"}, key = "#accessToken.id+'::'+#accessToken.refreshToken")
    Boolean removeToken(AccessToken accessToken);

}
